package com.www.applyword.ui.chat;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.tencent.imsdk.TIMFriendshipManager;
import com.tencent.imsdk.TIMUserProfile;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.imsdk.friendship.TIMFriendRequest;
import com.tencent.imsdk.friendship.TIMFriendResult;
import com.tencent.imsdk.friendship.TIMFriendStatus;
import com.www.applyword.R;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.utils.ImageUtil;
import com.www.applyword.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * author : huang
 * date   : 2019/12/14 0014 14:14
 * version: 1.0
 */
public class FriendProfileMoreActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.tv_my_photo)
    CircleImageView tvMyPhoto;
    @BindView(R.id.tv_my_name)
    TextView tvMyName;
    @BindView(R.id.tv_my_phone)
    TextView tvMyPhone;
    @BindView(R.id.btnAdd)
    TextView btnAdd;
    @BindView(R.id.et_info)
    EditText et_info;
    private String id;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.contact_friend_profile_activity2);
        setTitle("好友资料");
        btnAdd.setOnClickListener(this);
    }

    @Override
    protected void loadData() {
        id = getIntent().getStringExtra("id");
        List<String> identifiers = new ArrayList<>();
        identifiers.add(id);
        TIMFriendshipManager.getInstance().getUsersProfile(identifiers, true, new TIMValueCallBack<List<TIMUserProfile>>() {
            @Override
            public void onError(int i, String s) {
                Utils.showToastSafe("用户不存在");
            }

            @Override
            public void onSuccess(List<TIMUserProfile> timUserProfiles) {
                for (TIMUserProfile timUserProfile : timUserProfiles) {
                    ImageUtil.setImage(tvMyPhoto,timUserProfile.getFaceUrl());
                    tvMyName.setText(timUserProfile.getNickName());
                    tvMyPhone.setText(timUserProfile.getIdentifier());
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        TIMFriendRequest timFriendRequest = new TIMFriendRequest(id);
        timFriendRequest.setAddWording(et_info.getText().toString().trim());
        timFriendRequest.setAddSource("android");
        TIMFriendshipManager.getInstance().addFriend(timFriendRequest, new TIMValueCallBack<TIMFriendResult>() {
            @Override
            public void onError(int i, String s) {
                //    DemoLog.e(TAG, "addFriend err code = " + i + ", desc = " + s);
                // ToastUtil.toastShortMessage("Error code = " + i + ", desc = " + s);
                Utils.showToastSafe("用户不存在");
            }

            @Override
            public void onSuccess(TIMFriendResult timFriendResult) {
                //  DemoLog.i(TAG, "addFriend success result = " + timFriendResult.toString());
                switch (timFriendResult.getResultCode()) {
                    case TIMFriendStatus.TIM_FRIEND_STATUS_SUCC:
                        Utils.showToastSafe("添加好友成功");
                        break;
                    case TIMFriendStatus.TIM_FRIEND_PARAM_INVALID:
                        if (TextUtils.equals(timFriendResult.getResultInfo(), "Err_SNS_FriendAdd_Friend_Exist")) {
                            Utils.showToastSafe("对方已是您的好友");
                            break;
                        }
                    case TIMFriendStatus.TIM_ADD_FRIEND_STATUS_SELF_FRIEND_FULL:
                        Utils.showToastSafe("您的好友数已达系统上限");
                        break;
                    case TIMFriendStatus.TIM_ADD_FRIEND_STATUS_THEIR_FRIEND_FULL:
                        Utils.showToastSafe("对方的好友数已达系统上限");
                        break;
                    case TIMFriendStatus.TIM_ADD_FRIEND_STATUS_IN_SELF_BLACK_LIST:
                        Utils.showToastSafe("被加好友在自己的黑名单中");
                        break;
                    case TIMFriendStatus.TIM_ADD_FRIEND_STATUS_FRIEND_SIDE_FORBID_ADD:
                        Utils.showToastSafe("对方已禁止加好友");
                        break;
                    case TIMFriendStatus.TIM_ADD_FRIEND_STATUS_IN_OTHER_SIDE_BLACK_LIST:
                        Utils.showToastSafe("您已被被对方设置为黑名单");
                        break;
                    case TIMFriendStatus.TIM_ADD_FRIEND_STATUS_PENDING:
                        Utils.showToastSafe("等待好友审核同意");
                        break;
                    default:
                        Utils.showToastSafe(timFriendResult.getResultCode() + " " + timFriendResult.getResultInfo());
                        break;
                }
                finish();
            }
        });
    }
}
